// 引入接口 发起ajax请求时触发同源策略，实现跨域请求
import request from '../utils/request'
import store from '@/store'

// 获取所有频道接口
export const allChannelListAPI = () => {
  return request({
    url: '/v1_0/channels'
  })
}

// 登录接口
export const loginAPI = ({ mobile, code }) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}

// 获取用户列表
export const userChannelAPI = () => {
  return request({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: 'Bearer' + store.state.token
    // }
  })
}

// 获取文章新闻推荐
export const articlesListAPI = ({ channelId, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    method: 'GET',
    params: {
      channel_id: channelId,
      // 时间戳
      timestamp: timestamp || (new Date()).getTime()
    }
  })
}

// 对文章不喜欢
export const articleDislikesAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target: target
    }
    // headers: {
    //   Authorization: 'Bearer ' + store.state.token
    // }
  })
}

// 举报内容
export const articleReportsAPI = ({ target, type, remark }) => {
  return request({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target,
      type,
      remark
    }
  })
}

// // 频道 - 更新已选
export const updateChannelAPI = ({ channels }) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels: channels
    }
  })
}

// 获取联想建议
export const suggestionlAPI = ({ q }) => {
  return request({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: {
      q
    }
  })
}

// 获取搜索结果
export const searchAPI = ({ q, page = 1 }) => {
  return request({
    url: '/v1_0/search',
    method: 'GET',
    params: {
      q,
      page
    }
  })
}

// 获取新闻详情
export const articleDetailAPI = ({ id }) => {
  return request({
    url: `/v1_0/articles/${id}`
  })
}

// 关注用户
export const followingslAPI = ({ target }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: target
    }
  })
}

// 取消关注用户
export const unfollowingslAPI = ({ uid }) => {
  return request({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/user/followings/${uid}`,
    method: 'DELETE',
    data: {
      uid: uid
    }
  })
}

// 点赞文章
export const articleLikeAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: target
    }
  })
}

// 取消点赞
export const articleDisLikeAPI = ({ artId }) => {
  return request({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}

// 获取评论或评论回复
export const commentsAPI = ({ artId, offset, limit }) => {
  return request({
    url: '/v1_0/comments',
    method: 'GET',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

// 评论=发布
export const sendCommentAPI = ({ artId, content }) => {
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: artId,
      content: content
    }
  })
}

// 获取用户自己信息
export const userInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 获取用户个人资料
export const userProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 编辑用户照片资料（头像、身份证照片）
export const updatePhotoAPI = (formObj) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
  })
}

// 编辑用户个人资料
export const updateProfileAPI = ({ birthday, userName }) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday: birthday,
      name: userName
    }
  })
}

// 用户更新token
export const refreshTokenAPI = () => {
  return request({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${store.state.refresh_token}`
    }
  })
}
